<template>
  <div class="app-container">
    <!--搜索栏-->
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
      <el-form-item label="关键字搜索" label-width="85" prop="keyValue">
        <el-input
          v-model="queryParams.keyValue"
          placeholder="请输入搜索关键字"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属部门:" label-width="140px" prop="deptId">
        <el-tree-select
          v-model="queryParams.deptId"
          :data="deptOptions"
          :props="{ value: 'id', label: 'label', children: 'children' }"
          value-key="id"
          placeholder="请选择部门"
          check-strictly
        />
      </el-form-item>
      <el-form-item label="电子围栏等级:" label-width="140px" prop="doorType">
        <el-select v-model="queryParams.fenceRiskLevel" placeholder="请选择电子围栏等级">
          <el-option
            v-for="dict in fenceLevelOption"
            :key="dict.id"
            :label="dict.label"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!--数据列表-->
    <el-table v-loading="loading" :data="dataList">
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column label="电子围栏编号" width="120" prop="fenceCode">
        <template #default="scope">
          <span>{{ scope.row.fenceCode ? scope.row.fenceCode : "---" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="电子围栏名称" width="180" prop="fenceName" />
      <el-table-column label="电子围栏等级" width="180" prop="fenceRiskLevel">
        <template #default="scope">
          <span>{{
            scope.row.fenceRiskLevel ? parseLevel(scope.row.fenceRiskLevel) : "---"
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属部门" width="140" prop="doorCode" />
      <el-table-column label="高度" width="80" prop="fenceHeight" />
      <el-table-column label="点位" prop="fencePange" :show-overflow-tooltip="true" />

      <el-table-column label="创建时间" prop="createdAt">
        <template #default="scope">
          <span>{{ scope.row.createdAt ? parseTime(scope.row.createdAt) : "---" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="180" prop="fenceStart">
        <template #default="scope">
          <el-switch
            v-model="scope.row.fenceStart"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="180"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip content="分配白名单用户" placement="top">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['risk:event:edit']"
              >分配白名单</el-button
            >
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['risk:event:remove']"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.current"
      v-model:limit="queryParams.size"
      @pagination="getList"
    />
    <!--添加电子围栏白名单对话框-->
    <el-dialog :title="title" v-model="open" width="60%" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules"> </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="ScreenDoor">
import { listFence, deleteFence, changeFenceStatus } from "@/api/screen/fence";
import { deptTreeSelect } from "@/api/system/user";
const total = ref(0);
const loading = ref(false);
const dataList = ref([]); //数据列表
const title = ref("");
const open = ref(false);
const { proxy } = getCurrentInstance();
const deptOptions = ref([]);
const fenceLevelOption = ref([
  { id: 1, label: "红" },
  { id: 2, label: "橙" },
  { id: 3, label: "黄" },
  { id: 4, label: "蓝" },
]);

const data = reactive({
  form: {
    typeId: undefined,
  },
  queryParams: {
    current: 1,
    size: 10,
    keyValue: undefined,
  },
  rules: {},
});
const { queryParams, form, rules } = toRefs(data);

/** 解析电子围栏等级 */
function parseLevel(val) {
  var itemTarget = fenceLevelOption.value.filter((item) => {
    return item.id == val;
  });
  return itemTarget[0].label;
}

/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then((response) => {
    deptOptions.value = response.data;
  });
}

/** 更改电子围栏状态 */
function handleStatusChange(row) {
  let text = row.fenceStart === "0" ? "启用" : "停用";
  proxy.$modal
    .confirm('确认要"' + text + '""' + row.fenceName + '"电子围栏吗?')
    .then(function () {
      return changeFenceStatus(row.fenceId);
    })
    .then(() => {
      proxy.$modal.msgSuccess(text + "成功");
    })
    .catch(function () {
      row.fenceStart = row.fenceStart === "0" ? "1" : "0";
    });
}

/** 获取列表数据 */
function getList() {
  loading.value = true;
  listFence(queryParams.value).then((response) => {
    loading.value = false;
    dataList.value = response.data.dataList;
    total.value = response.data.totalNum;
  });
}
/** 重置 */
function reset() {
  form.value = {};
  proxy.resetForm("formRef");
}

/** 处理删除 */
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除名称为"' + row.fenceName + '"的数据项?')
    .then(function () {
      return deleteFence(row.fenceId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 处理取消 */
function cancel() {
  open.value = false;
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

getList();
getDeptTree();
</script>
